package com.leruan.aasimp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.bean.CarBasicCarTypeBean;
import com.leruan.aasimp.bean.CarBasicDemioTypesBean;
import com.leruan.aasimp.dao.CarBasicInfoDao;
import com.leruan.aasimp.entity.CarBasicInfo;

public class CarBasicInfoDaoImpl extends HibernateDaoSupport implements
		CarBasicInfoDao {

	public void save(CarBasicInfo cardBasicInfo) throws Exception {

		Session session = this.getSession();
		session.save(cardBasicInfo);
	}

	public List<String> getBrandList() {
		Session session = this.getSession();
		String hql = "select distinct brand from CarBasicInfo order by brand";
		return session.createQuery(hql).list();

		// List<UsedCarBrandBean> uList = new ArrayList<UsedCarBrandBean>();
		//		
		// for(int i = 0;i < list.size(); i++){
		//			
		// List<UsedCarDepartmentBean> department = new
		// ArrayList<UsedCarDepartmentBean>();
		// UsedCarBrandBean uBBean = new UsedCarBrandBean();
		// UsedCarDepartmentBean uDBean = new UsedCarDepartmentBean();
		//			
		// Object[] object = (Object[]) list.get(i);
		//			
		// boolean isAddBrand = true;
		//			
		// for(int j = 0;j < uList.size(); j++){
		// if(uList.get(j).getBrand().equals(object[0].toString()))
		// {
		// isAddBrand = false;
		// break;
		// }
		// }
		// if(isAddBrand){
		// List<String> carType = new ArrayList<String>();
		// carType.add(object[2].toString());
		// uDBean.setCarType(carType);
		// uDBean.setDepartment(object[1].toString());
		// uBBean.setBrand(object[0].toString());
		// uList.add(uBBean);
		// continue;
		// }
		// for(int j = 0; j){}

		// }
	}

	public List<String> getDepartmentListByBrand(String brand) throws Exception {
		Session session = this.getSession();
		String hql = "select distinct department from CarBasicInfo where brand = ? order by department";
		return session.createQuery(hql).setString(0, brand).list();
	}

	public List<String> getCarTypeListByDepartment(String department)
			throws Exception {
		Session session = this.getSession();
		String hql = "select distinct carType from CarBasicInfo where department = ? order by carType";
		return session.createQuery(hql).setString(0, department).list();
	}

	public CarBasicInfo getCarBasicInfoById(String id) throws Exception {
		Session session = this.getSession();
		String hql = "from CarBasicInfo where id = ?";
		return (CarBasicInfo) session.createQuery(hql).setString(0, id)
				.uniqueResult();
	}

	public List<CarBasicDemioTypesBean> getDemioTypesList() throws Exception {
		
		Session session = this.getSession();
		String hql = "select distinct brand,department,demioType from CarBasicInfo order by demioType";
		List list = session.createQuery(hql).list();
		List<CarBasicDemioTypesBean> demioTypesList = new ArrayList<CarBasicDemioTypesBean>();
		
		for(int i = 0;i < list.size(); i++){
			CarBasicDemioTypesBean cBean = new CarBasicDemioTypesBean(); 
			Object[] object = (Object[]) list.get(i);
			cBean.setBrand(object[0].toString());
			cBean.setDepartment(object[1].toString());
			cBean.setDemioTypes(object[2].toString());
			demioTypesList.add(cBean);
		}
		return demioTypesList;
	}

	public List<CarBasicCarTypeBean> getCarTypes(String demioType, String carType)
			throws Exception {
		
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("select id,carType,directMoney,imageAddress,engine,dispalcement,marketTime from CarBasicInfo where demioType = ?");
		if(carType != null){
			sb.append(" and carType = ?");
		}
		Query query = session.createQuery(sb.toString()).setString(0, demioType);
		if(carType != null)
			query = query.setString(1, carType);
		List list = query.list();
		List<CarBasicCarTypeBean> tlist = new ArrayList<CarBasicCarTypeBean>();
		for(int i = 0;i < list.size(); i++){
			CarBasicCarTypeBean tBean = new CarBasicCarTypeBean();
			Object[] object = (Object[]) list.get(i);
			tBean.setId(object[0].toString());
			tBean.setCarType(object[1].toString());
			tBean.setDirectMoney(object[2].toString());
			tBean.setImageAddress(object[3].toString());
			tBean.setEngine(object[4].toString());
			tBean.setDispalcement(object[5].toString());
			tBean.setMarketTime((Date)object[6]);
			tlist.add(tBean);
		}
		return  tlist;
	}

	public List<String> getImagesUrlById(String id) throws Exception {
		Session session = this.getSession();
		String hql = "select imageAddress from CarBasicInfoImage where carBasicId = ?";
		List<String> list = (List<String>) session.createQuery(hql).setString(0, id).list();
		return list;
	}

	public Long getCarBasicId(String brand, String department, String carType)
			throws Exception {
		Session session = this.getSession();
		String hql = "select id from CarBasicInfo where brand=? and department=? and carType=?";
		Query query = session.createQuery(hql).setString(0, brand).setString(1, department).setString(2, carType);
		return (Long) query.uniqueResult();
	}

}
